//
//  QuestionView.m
//  IssuesGame
//
//  Created by Martin Grider on 10/21/09.
//  Copyright 2009 Regents of the University of Minnesota
//

#import "QuestionView.h"


@implementation QuestionView

@synthesize gameController, buttonLeft, buttonRight;
@synthesize perspectiveTitle, perspectiveImage, questionText, answerText1, answerText2, answerText3, answerText4;

- (id)initWithFrame:(CGRect)frame andController:(UIViewController *)viewController {
    if (self = [super initWithFrame:frame]) {

		[self setBackgroundColor:[UIColor clearColor]];

		// some "static" images and buttons
		UIImageView *bg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_overlay_question.png"]];
		[bg setFrame:CGRectMake(0, 0, 320, 324)];
		[bg setBackgroundColor:[UIColor clearColor]];
		[self addSubview:bg];
		[bg release];

		buttonLeft = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
		[buttonLeft setImage:[UIImage imageNamed:@"question_arrow_left.png"] forState:UIControlStateNormal];
		[buttonLeft setAlpha:kQuestionArrowMinimumAlpha];
		[buttonLeft setFrame:CGRectMake(9, 213, 14, 28)];
		[buttonLeft setBackgroundColor:[UIColor clearColor]];
		[buttonLeft addTarget:self action:@selector(slideAnswerRight) forControlEvents:UIControlEventTouchUpInside];
		[self addSubview:buttonLeft];

		buttonRight = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
		[buttonRight setImage:[UIImage imageNamed:@"question_arrow_right.png"] forState:UIControlStateNormal];
		[buttonRight setFrame:CGRectMake(297, 213, 14, 28)];
		[buttonRight setBackgroundColor:[UIColor clearColor]];
		[buttonRight addTarget:self action:@selector(slideAnswerLeft) forControlEvents:UIControlEventTouchUpInside];
		[self addSubview:buttonRight];

		UIButton *selectAnswerbutton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
		[selectAnswerbutton setBackgroundImage:[UIImage imageNamed:@"btn_grey.png"] forState:UIControlStateNormal];
		[selectAnswerbutton setFrame:CGRectMake(43, 243, 234, 26)];
		[selectAnswerbutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
		selectAnswerbutton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:12];
		[selectAnswerbutton setTitle:@"Select This Answer" forState:UIControlStateNormal];
		[selectAnswerbutton addTarget:self action:@selector(selectingCurrentAnswer) forControlEvents:UIControlEventTouchUpInside];
		[self addSubview:selectAnswerbutton];
		[selectAnswerbutton release];

		// default text color 
		UIColor *defaultTextColor = [UIColor colorWithRed:kDefaultTextColorRed green:kDefaultTextColorGreen blue:kDefaultTextColorBlue alpha:1];

		// some "dynamic" stuff that will change
		perspectiveTitle = [[UILabel alloc] initWithFrame:CGRectMake(37, 37, 190, 15)];
		[perspectiveTitle setText:@"Perspective Title"];
		[perspectiveTitle setTextColor:[UIColor whiteColor]];
		[perspectiveTitle setBackgroundColor:[UIColor clearColor]];
		[perspectiveTitle setTextAlignment:UITextAlignmentLeft];
		[perspectiveTitle setFont:[UIFont fontWithName:@"Helvetica-Bold" size:12]];
		[self addSubview:perspectiveTitle];

		perspectiveImage = [[UIImageView alloc] initWithImage:nil];
		[perspectiveImage setFrame:CGRectMake(253, 21, 41, 41)];
		[self addSubview:perspectiveImage];

		UIImageView *tileGlossView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"tile_overlay.png"]];
		[tileGlossView setFrame:CGRectMake(253, 21, 41, 41)];
		[self addSubview:tileGlossView];
		[tileGlossView release];

		questionText = [[UILabel alloc] initWithFrame:CGRectMake(43, 75, 240, 81)];
		[questionText setText:@"Question Text"];
		[questionText setNumberOfLines:0];
		[questionText setTextColor:defaultTextColor];
		[questionText setBackgroundColor:[UIColor clearColor]];
		[questionText setTextAlignment:UITextAlignmentLeft];
		[questionText setFont:[UIFont fontWithName:@"Helvetica" size:16]];
		[self addSubview:questionText];

		// parent view for the answers
		UIView *answerParentBoundry = [[UIView alloc] initWithFrame:CGRectMake(43, 170, 234, 70)];
		[answerParentBoundry setClipsToBounds:YES];
		[answerParentBoundry setTag:100];
		[self addSubview:answerParentBoundry];
		[answerParentBoundry release];

		answerText1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 234, 70)];
		[answerText1 setText:@"Answer Text 1"];
		[answerText1 setNumberOfLines:0];
		[answerText1 setTextColor:defaultTextColor];
		[answerText1 setBackgroundColor:[UIColor clearColor]];
		[answerText1 setTextAlignment:UITextAlignmentLeft];
		[answerText1 setFont:[UIFont fontWithName:@"Helvetica" size:12]];
		[[self viewWithTag:100] addSubview:answerText1];

		answerText2 = [[UILabel alloc] initWithFrame:CGRectMake(240, 0, 234, 70)];
		[answerText2 setText:@"Answer Text 1"];
		[answerText2 setNumberOfLines:0];
		[answerText2 setTextColor:defaultTextColor];
		[answerText2 setBackgroundColor:[UIColor clearColor]];
		[answerText2 setTextAlignment:UITextAlignmentLeft];
		[answerText2 setFont:[UIFont fontWithName:@"Helvetica" size:12]];
		[[self viewWithTag:100] addSubview:answerText2];
		
		answerText3 = [[UILabel alloc] initWithFrame:CGRectMake(240, 0, 234, 70)];
		[answerText3 setText:@"Answer Text 1"];
		[answerText3 setNumberOfLines:0];
		[answerText3 setTextColor:defaultTextColor];
		[answerText3 setBackgroundColor:[UIColor clearColor]];
		[answerText3 setTextAlignment:UITextAlignmentLeft];
		[answerText3 setFont:[UIFont fontWithName:@"Helvetica" size:12]];
		[[self viewWithTag:100] addSubview:answerText3];
		
		answerText4 = [[UILabel alloc] initWithFrame:CGRectMake(240, 0, 234, 70)];
		[answerText4 setText:@"Answer Text 1"];
		[answerText4 setNumberOfLines:0];
		[answerText4 setTextColor:defaultTextColor];
		[answerText4 setBackgroundColor:[UIColor clearColor]];
		[answerText4 setTextAlignment:UITextAlignmentLeft];
		[answerText4 setFont:[UIFont fontWithName:@"Helvetica" size:12]];
		[[self viewWithTag:100] addSubview:answerText4];

		// faking a UIPageControl
		UIImageView *pageControl1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"page_selected.png"]];
		[pageControl1 setFrame:CGRectMake(133, 285, 6, 6)];
		[pageControl1 setTag:801];
		[self addSubview:pageControl1];
		[pageControl1 release];
		UIImageView *pageControl2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"page_unselected.png"]];
		[pageControl2 setFrame:CGRectMake(151, 285, 6, 6)];
		[pageControl2 setTag:802];
		[self addSubview:pageControl2];
		[pageControl2 release];
		UIImageView *pageControl3 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"page_unselected.png"]];
		[pageControl3 setFrame:CGRectMake(169, 285, 6, 6)];
		[pageControl3 setTag:803];
		[self addSubview:pageControl3];
		[pageControl3 release];
		UIImageView *pageControl4 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"page_unselected.png"]];
		[pageControl4 setFrame:CGRectMake(187, 285, 6, 6)];
		[pageControl4 setTag:804];
		[self addSubview:pageControl4];
		[pageControl4 release];

		currentAnswerIndex = 0;
		animatingAnswer = NO;

		// we'll need this later.
		[self setGameController:viewController];
    }
    return self;
}


- (void)loadQuestionFromTile:(Tile *)tile {

	[perspectiveTitle setText:[[tile perspective] capitalizedString]];
	[questionText setText:[tile question]];
	[perspectiveImage setImage:[UIImage imageWithContentsOfFile:[tile imagePath]]];
	[answerText1 setText:[[tile answers] objectAtIndex:0]];
	[answerText2 setText:[[tile answers] objectAtIndex:1]];
	[answerText3 setText:[[tile answers] objectAtIndex:2]];
	[answerText4 setText:[[tile answers] objectAtIndex:3]];

	correctAnswerIndex = [tile answer];

	// this basically just sets the first answer as selected
	[self resetQuestionSelection];
}


- (void)resetQuestionSelection {

	currentAnswerIndex = 0;
	[buttonLeft setAlpha:kQuestionArrowMinimumAlpha];
	[buttonRight setAlpha:1.0];
	[answerText1 setFrame:CGRectMake(0, 0, 234, 70)];
	[answerText2 setFrame:CGRectMake(240, 0, 234, 70)];
	[answerText3 setFrame:CGRectMake(240, 0, 234, 70)];
	[answerText4 setFrame:CGRectMake(240, 0, 234, 70)];
	[(UIImageView *)[self viewWithTag:801] setImage:[UIImage imageNamed:@"page_selected.png"]];
	[(UIImageView *)[self viewWithTag:802] setImage:[UIImage imageNamed:@"page_unselected.png"]];
	[(UIImageView *)[self viewWithTag:803] setImage:[UIImage imageNamed:@"page_unselected.png"]];
	[(UIImageView *)[self viewWithTag:804] setImage:[UIImage imageNamed:@"page_unselected.png"]];
}


- (void)selectingCurrentAnswer {

	if ( animatingAnswer ) {
		return;
	}

	//NSLog(@"selecting current answer...");

	if ( currentAnswerIndex == correctAnswerIndex ) {
		//NSLog(@"correct!");
		[gameController changePlayerScore:kGameScoreQuestionCorrectIncrement];
		[gameController openCorrectAnswerView];
	}
	else {
		//NSLog(@"incorrect...  :(");
		[self slideToCorrectAnswer];
		[gameController openIncorrectAnswerView];
	}
}


- (void)slideToCorrectAnswer {

	animatingAnswer = YES;

	selectedImageTag = (correctAnswerIndex + 801);
	unselectedImageTag = (currentAnswerIndex + 801);

	UILabel *currentAnswer, *correctAnswer;
	if ( currentAnswerIndex == 0 ) {
		currentAnswer = answerText1;
	}
	else if (currentAnswerIndex == 1 ) {
		currentAnswer = answerText2;
	}
	else if (currentAnswerIndex == 2 ) {
		currentAnswer = answerText3;
	}
	else if (currentAnswerIndex == 3 ) {
		currentAnswer = answerText4;
	}
	if ( correctAnswerIndex == 0 ) {
		correctAnswer = answerText1;
	}
	else if ( correctAnswerIndex == 1 ) {
		correctAnswer = answerText2;
	}
	else if ( correctAnswerIndex == 2 ) {
		correctAnswer = answerText3;
	}
	else if ( correctAnswerIndex == 3 ) {
		correctAnswer = answerText4;
	}

	// animate the transition
	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
	[UIView setAnimationDuration:kAnswerAnimationInterval];
	[UIView setAnimationDelegate:self];
	[UIView setAnimationDidStopSelector:@selector(endImageAnimation)];

	if ( currentAnswerIndex < correctAnswerIndex ) {
		[currentAnswer setFrame:CGRectMake(-240, 0, 234, 70)];
		[correctAnswer setFrame:CGRectMake(0, 0, 234, 70)];
	}
	else {
		[currentAnswer setFrame:CGRectMake(240, 0, 234, 70)];
		[correctAnswer setFrame:CGRectMake(0, 0, 234, 70)];
	}

	[UIView commitAnimations];
}


- (void)slideAnswerLeft {
	//NSLog(@"sliding answer left...");

	if ( animatingAnswer ) {
		return;
	}
	animatingAnswer = YES;

	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
	[UIView setAnimationDuration:kAnswerAnimationInterval];
	[UIView setAnimationDelegate:self];
	[UIView setAnimationDidStopSelector:@selector(endImageAnimation)];

	if ( currentAnswerIndex == 0 ) {
		[answerText1 setFrame:CGRectMake(-240, 0, 234, 70)];
		[answerText2 setFrame:CGRectMake(0, 0, 234, 70)];
		[buttonLeft setAlpha:1.0];
		selectedImageTag = 802;
		unselectedImageTag = 801;
		currentAnswerIndex++;
	}
	else if ( currentAnswerIndex == 1 ) {
		[answerText2 setFrame:CGRectMake(-240, 0, 234, 70)];
		[answerText3 setFrame:CGRectMake(0, 0, 234, 70)];
		selectedImageTag = 803;
		unselectedImageTag = 802;
		currentAnswerIndex++;
	}
	else if ( currentAnswerIndex == 2 ) {
		[answerText3 setFrame:CGRectMake(-240, 0, 234, 70)];
		[answerText4 setFrame:CGRectMake(0, 0, 234, 70)];
		selectedImageTag = 804;
		unselectedImageTag = 803;
		[buttonRight setAlpha:kQuestionArrowMinimumAlpha];
		currentAnswerIndex++;
	}
	else if ( currentAnswerIndex == 3 ) {
		// do nothing, we're all the way left
	}
	[UIView commitAnimations];
}


- (void)slideAnswerRight {
	//NSLog(@"sliding answer right...");

	if ( animatingAnswer ) {
		return;
	}
	animatingAnswer = YES;

	[UIView beginAnimations:nil context:nil];
	[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
	[UIView setAnimationDuration:kAnswerAnimationInterval];
	[UIView setAnimationDelegate:self];
	[UIView setAnimationDidStopSelector:@selector(endImageAnimation)];
	
	if ( currentAnswerIndex == 0 ) {
		// do nothing, we're all the way right
	}
	else if ( currentAnswerIndex == 1 ) {
		[answerText1 setFrame:CGRectMake(0, 0, 234, 70)];
		[answerText2 setFrame:CGRectMake(240, 0, 234, 70)];
		selectedImageTag = 801;
		unselectedImageTag = 802;
		[buttonLeft setAlpha:kQuestionArrowMinimumAlpha];
		currentAnswerIndex--;
	}
	else if ( currentAnswerIndex == 2 ) {
		[answerText2 setFrame:CGRectMake(0, 0, 234, 70)];
		[answerText3 setFrame:CGRectMake(240, 0, 234, 70)];
		selectedImageTag = 802;
		unselectedImageTag = 803;
		currentAnswerIndex--;
	}
	else if ( currentAnswerIndex == 3 ) {
		[answerText3 setFrame:CGRectMake(0, 0, 234, 70)];
		[answerText4 setFrame:CGRectMake(240, 0, 234, 70)];
		selectedImageTag = 803;
		unselectedImageTag = 804;
		[buttonRight setAlpha:1.0];
		currentAnswerIndex--;
	}
	[UIView commitAnimations];
}


- (void)endImageAnimation {
	[(UIImageView *)[self viewWithTag:unselectedImageTag] setImage:[UIImage imageNamed:@"page_unselected.png"]];
	[(UIImageView *)[self viewWithTag:selectedImageTag] setImage:[UIImage imageNamed:@"page_selected.png"]];
	animatingAnswer = NO;
}


- (void)drawRect:(CGRect)rect {
    // Drawing code
}


- (void)dealloc {
    [super dealloc];
}


@end
